package cate.game.role.pvp.throne;

import cate.common.table.activity.tbboss.TBChampionBossRow;
import cate.common.table.d.GDFight;
import cate.common.table.d.GDFunc;
import cate.game.attr.FightAttr;
import cate.game.play.param.HpExtend;
import cate.game.play.part.FightSide;
import cate.game.play.proce.play.PlayPO;
import cate.game.pvp.throne.TBChampionBossFTC;
import cate.game.pvp.throne.msg.TBBossCheckInnerResp;
import cate.game.role.farm.AbstractFarm;
import cate.game.role.fight.Position;
import easy.java.dev.note.NoteClass;
import easy.java.practice.errorcode.EcResult;

import java.util.stream.Collectors;

@NoteClass("冠军boss")
public class RoleTBChampionBoss extends AbstractFarm {


	@Override
	public int getFuncId() {
		return GDFunc.THRONE_CHAMPION_BOSS;
	}

	@Override
	public void onAfterOpen() {

	}

	@Override
	public int getFightFuncId() {
		return GDFunc.THRONE_CHAMPION_BOSS;
	}

	public void onSeasonChange(){

	}

	public EcResult<Void> challenge(boolean sim) {
		EcResult<Void> r = new EcResult<>();
		if(isFighting()){
			return r.fail("当前正在战斗中");
		}
		TBChampionBossRow bossRow = game().table.tbBoss.champion.getFirst();
		if (bossRow == null) {
			return r.fail("boss配置不存在");
		}
		Position position = role.getFight().getPosition(GDFunc.GRADE_PVP, 1);
		if (position == null) {
			return r.fail("尚未布阵");
		}
		TBBossCheckInnerResp resp = game().pvp.throne.message.bossCheck(role.getUid(), position.grids.stream().map(g -> g.uid).collect(Collectors.toList()));
		if (!resp.result.ok()) {
			return r.fail(resp.result.message);
		}
		EcResult<FightSide> sideB_r = game().fight.side.createFightSideByCode(GDFight.Team.B, bossRow.insCode);
		if (!sideB_r.ok()) {
			return r.fail(sideB_r.message);
		}
		FightSide sideB = sideB_r.data;
		HpExtend hpExtend = game().pvp.throne.data.holder.boss.hpExtend.copy();

		TBChampionBossFTC ftc = new TBChampionBossFTC(role, sim, bossRow, hpExtend);
		ftc.setPositionApply(GDFunc.GRADE_PVP);
		ftc.param.sideB.hpExtend = hpExtend;

		final int killTime = game().pvp.throne.data.holder.boss.totalKillTime;
		if (killTime > 0) {
			ftc.param.sideB.attrAppend.addHeroAttr(bossRow.bossTid, new FightAttr(bossRow.attrStr).scale(killTime));
		}
		EcResult<PlayPO> play_r = game().play.startByRole(role, ftc, sideB);
		if (!play_r.ok()) {
			return r.fail(play_r.message);
		}
		this.startFighting();
		return r.success();
	}
}
